<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="600px">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :rules="rules"
      :select-list="selectList"
      :search-tags="searchTags"/>
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        type="primary"
        icon="el-icon-receiving"
        @click="handleSave"
      >{{ $t('save') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAdd, doUpdate, getInfo, getIdAndToolingModelList, getProductList } from '@/api/configSetting/toolingProductBingding'
export default {
  name: 'BindProductForm',
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: {},
      selectList: {},
      searchTags:
        [
          { label: this.$t('toolingModel'), prop: 'toolingModelId', tag: 'DynSelect', style: { width: '100%' }, disabled: false },
          { label: this.$t('productName'), prop: 'productNames', tag: 'DynSelect', multiple: true, style: { width: '100%' }}
        ],
      rules: Object.freeze(
        {
          toolingModelId: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('toolingModel')}`, trigger: 'change' }],
          productNames: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('productName')}`, trigger: 'change' }]
        }
      )
    }
  },
  methods: {
    handleOpen(row) {
      if (row && row.id) {
        this.title = this.$t('modify')
        this.$set(this.searchTags, 0, { label: this.$t('toolingModel'), prop: 'toolingModelId', tag: 'DynSelect', style: { width: '100%' }, disabled: true })
        getInfo({ id: row.id }).then(res => {
          const { id, toolingModelId, productNamesList } = res.data
          this.searchForm = { id, toolingModelId, productNames: productNamesList }
        })
      } else {
        this.title = this.$t('add')
      }
      this.getIdAndToolingModelList()
      this.getProductIdList()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    },
    handleSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            doUpdate(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          } else {
            doAdd(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    // 获取设备类型列表
    async getIdAndToolingModelList() {
      const res = await getIdAndToolingModelList()
      this.$set(this.selectList, 'toolingModelId', res.data)
    },
    async getProductIdList() {
      const res = await getProductList()
      this.$set(this.selectList, 'productNames', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-form .el-form-item{
  width: 100%;
   .el-form-item__content{
    width: calc(100% - 120px);
  }
}
::v-deep .el-select .el-select__tags .el-tag{
  display: flex;
  justify-content: space-between;
  align-items: center;
  .el-select__tags-text{
    display: inline-block;
    max-width: 340px;
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
}
}
</style>
